package com.poisonh.unifiedauth.app;

import android.app.Application;
import android.os.Environment;

import com.lzy.okhttputils.OkHttpUtils;
import com.lzy.okhttputils.model.HttpHeaders;

import java.io.File;
import java.lang.reflect.Field;

import cn.jpush.android.api.JPushInterface;
import io.realm.Realm;
import io.realm.RealmConfiguration;

/**
 * Created by PoisonH on 2016/6/27.
 */
public class MyApplication extends Application
{
//    //在自己的Application中添加如下代码
//    private RefWatcher refWatcher;
//
//    public static RefWatcher getRefWatcher(Context context)
//    {
//        MyApplication application = (MyApplication) context.getApplicationContext();
//        return application.refWatcher;
//    }

    @Override
    public void onCreate()
    {
        super.onCreate();

        //在自己的Application中添加如下代码
//        refWatcher = LeakCanary.install(this);

        JPushInterface.setDebugMode(true);
        JPushInterface.init(this);

        OkHttpUtils.init(this);
        try
        {
            //配置网络请求头
            HttpHeaders commonHeaders = new HttpHeaders();
            commonHeaders.put("APIVersion", "1.0");
            commonHeaders.put("agreementVersion", "https");
            OkHttpUtils.getInstance().addCommonHeaders(commonHeaders)
                    .setCertificates(getAssets().open("30cer.com.jks"));
            //配饰realm数据库
            RealmConfiguration config = new RealmConfiguration.Builder(this)
                    .name("poisonh.realm")
                    .deleteRealmIfMigrationNeeded()
                    .schemaVersion(1)
                    // .migration(new Migration())
                    .build();
//            RealmConfiguration.Builder b =  new RealmConfiguration.Builder(this)
//                    .name("poisonh.realm")
//                    .deleteRealmIfMigrationNeeded()
//                    .schemaVersion(1);
//            Class<? extends RealmConfiguration.Builder> bs = b.getClass();
//            Field filed = bs.getDeclaredField("folder");
//            filed.setAccessible(true);
//            File ff = Environment.getExternalStorageDirectory();
//            filed.set(b,new File(Environment.getExternalStorageDirectory(),"nianlun"));
//            RealmConfiguration config = b.build();


            Realm.setDefaultConfiguration(config);
        } catch (Exception e)
        {
            e.printStackTrace();
        }
    }

}
